package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a02_分发饼干复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/2
 */
public class a02_分发饼干复习3 {
    public int findContentChildren(int[] children, int[] foods) {
        Arrays.sort(children);
        Arrays.sort(foods);

        //将最大的是食物分给饭量最大孩子
        int result = 0;
        int childIdx = children.length - 1;
        int foodIdx = foods.length - 1;
        while (foodIdx >= 0 && childIdx >= 0){
            if (foods[foodIdx] >= children[childIdx]) {//够吃：换孩子换食物，结果+1
                result++;
                childIdx--;
                foodIdx--;
            }else {//不够吃：换孩子不换食物
                childIdx--;
            }
        }
        return result;
    }
}
